export default class Base64Utils {

  /**
   * 将 Base64 格式的字符串转换为 ArrayBuffer
   * @param base64 - Base64 格式的字符串
   * @returns ArrayBuffer
   */
  public static base64ToBuffer(base64: string): ArrayBuffer {
    const binaryString = atob(base64)
    const bytes = new Uint8Array(binaryString.length)
    for (let i = 0; i < binaryString.length; i++) {
      bytes[i] = binaryString.charCodeAt(i)
    }
    return bytes.buffer
  }

  /**
   * 将 ArrayBuffer 转换为 Base64 格式的字符串
   * @param buffer - ArrayBuffer
   * @returns Base64 格式的字符串
   */
  public static bufferToBase64(buffer: ArrayBuffer): string {
    const bytes = new Uint8Array(buffer)
    const binaryString = bytes.reduce((s, byte) => s + String.fromCharCode(byte), '')
    return btoa(binaryString)
  }
}
